package com.warehouse.controller;


import com.warehouse.entity.po.*;
import com.warehouse.entity.pojo.Colour;
import com.warehouse.entity.pojo.Manufacturer;
import com.warehouse.result.Result;
import com.warehouse.service.impl.ColourServiceImpl;
import com.warehouse.service.impl.ManufacturerServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.soap.Addressing;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 生产厂家 前端控制器
 * </p>
 *
 * @author 卓汉杰
 * @since 2021-05-07
 */
@Api("生产厂家")
@CrossOrigin
@RestController
@RequestMapping("/manufacturer")
public class ManufacturerController {

    @Autowired
    ManufacturerServiceImpl manufacturerService;

    @ApiOperation(value = "添加生产厂家")
    @PostMapping("/insert")
    @ResponseBody
    public Result insert(@RequestBody AddManufacturerPO addManufacturerPO){

        Manufacturer manufacturer = new Manufacturer();

        manufacturer.setName(addManufacturerPO.getName());


        System.out.println(manufacturer);

        boolean save = manufacturerService.save(manufacturer);

        if(save){
            return new Result(null,Result.OK,"添加成功");
        }{
            return new Result(null,Result.NULL,"添加失败");
        }

    }


    @ApiOperation(value = "获取厂家列表")
    @GetMapping("/listPage")
    @ResponseBody
    public Result listPage(PagePO listPageColourPO){

        com.baomidou.mybatisplus.extension.plugins.pagination.Page page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page(listPageColourPO.getPageNum(),listPageColourPO.getPageSize());

        com.baomidou.mybatisplus.extension.plugins.pagination.Page page1 = manufacturerService.page(page);

        Map data = new HashMap();
        data.put("total",page1.getTotal());
        data.put("size",page1.getSize());
        data.put("manufacturer",page1.getRecords());
        return new Result(data,Result.OK,"请求成功");
    }


    @ApiOperation(value = "通过id获取厂家")
    @GetMapping("/getById")
    @ResponseBody
    public Result getById( Integer  id){
        Manufacturer manufacturer = manufacturerService.getById(id);

        if (manufacturer == null) {
            return new Result(null,Result.NULL,"该厂家不存在");
        }else {
            Map<String,Object> data = new HashMap<>();
            data.put("name",manufacturer.getName());
            return new Result(data,Result.OK,"请求成功");
        }


    }



    @ApiOperation(value = "修改生产厂家")
    @PostMapping("/update")
    @ResponseBody
    public Result update(@RequestBody UpManufacturerPO upManufacturerPO){
        Manufacturer manufacturer = new Manufacturer();
        manufacturer.setId(upManufacturerPO.getId());
        manufacturer.setName(  upManufacturerPO.getName());

        boolean save = manufacturerService.updateById(manufacturer);

        if(save){
            return new Result(null,Result.OK,"修改成功");
        }{
            return new Result(null,Result.NULL,"修改失败");
        }

    }



    @ApiOperation(value = "根据id删除厂家")
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody DeleteColourPO deleteColourPO){


        boolean save = manufacturerService.removeById(deleteColourPO.getId());

        if(save){
            return new Result(null,Result.OK,"删除成功");
        }{
            return new Result(null,Result.NULL,"删除失败");
        }

    }

}

